<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018-11-01
  Time: 23:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>就叫蛮小吉吧</title>
    <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
</head>
<body>
<div style="height: 500px;width: 400px;margin : auto 600px">
    <h1>蛮小吉</h1>

    账号：<input type="text"  id="username"></br>
    密码：<input type="password"  id="password" ></br>
    <button onclick=Ajax(0)>登 录</button>
    <button onclick=Ajax(1)>注 册</button>
</div>


</body>

<script type="text/javascript">
    var code;
    function Ajax(type){
        var username= $("#username").val();
        var password= $("#password").val();
        $.ajax({
            type:"post",
            url:"http://localhost:8080/homework",
            async:true,
            data:{
             "type":type,
             "username":username,
             "password":password
             },
            "success":function (data) {
                $("h2").remove();
            if(type==0){
                login(data);
            }else if (type==1){
                add(data);
            }

        },
        "error":function () {
            alert("连接失败");
        }
    });
    }
    function login(data){
                $.each(data,function(dataId,dataitem){
                     code= dataitem.code;
                     username=dataitem.username;
                     password=dataitem.password;
                });
                if(code==1){
                    if(username=="admin"){
                        $("div ").append("<h2>Welcome superadmin</h2>");
                    }
                    $("div").append("<h2>欢迎"+username+"登陆</h2>");
                    alert("欢迎"+username+"登陆");

                }else if(code==0) {
                    alert("账号密码错误");
                }
    }
    function add(data){
        var result=data.result;
        if(result==1){
            alert("注册成功");
        }else if(result==0){
            alert("注册失败");
        }
    }
</script>
</html>
